package com.bigshark.api.controller;

import com.bigshark.api.annotation.Login;
import com.bigshark.api.annotation.LoginUser;
import com.bigshark.api.form.LoginForm;
import com.bigshark.api.form.RegisterForm;
import com.bigshark.api.form.UserInfoForm;
import com.bigshark.api.model.User;
import com.bigshark.api.service.UserService;
import com.bigshark.common.utills.R;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/api/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 登陆
     * @param login
     * @return
     */
    @PostMapping(value = "/login")
    @ApiOperation(value = "登陆",notes = "用户登陆",tags = "用户接口",httpMethod = "POST")
    @ApiResponses({
            @ApiResponse(code=200,message="成功",response=R.class),
    })
    public R login(@RequestBody LoginForm login,@ApiIgnore HttpServletRequest request){
        Map<String,Object> map;
        try {
            map = userService.login(login,request);
        }catch (Exception e){
            e.printStackTrace();
            return R.error(e.getMessage());
        }
        return R.ok(map);
    }

    /**
     * 注册
     * @param registerForm
     * @return
     */
    @PostMapping(value = "/register")
    @ApiOperation(value = "注册",notes = "用户注册",tags = "用户接口",httpMethod = "POST")
    @ApiResponses({
            @ApiResponse(code=200,message="成功",response=R.class),
    })
    public R register(@RequestBody RegisterForm registerForm){
        try {
           userService.register(registerForm);
        }catch (Exception e){
            e.printStackTrace();
            return R.error(e.getMessage());
        }
        return R.ok();
    }

    /**
     * 修改用户信息
     * @param info
     * @return
     */
    @Login
    @PutMapping(value = "/")
    @ApiOperation(value = "修改信息",notes = "修改信息",tags = "用户接口",httpMethod = "PUT")
    public R setInfo(@RequestBody UserInfoForm info,@ApiIgnore @LoginUser User u){
        try {
            Assert.notNull(u,"用户不存在");
            u.setEmail(info.getEmail());
            u.setMobile(info.getMobile());
            u.setSex(info.getSex());
            userService.saveOrUpdate(u);
        }catch (Exception e){
            return R.error(e.getMessage());
        }
        return R.ok();
    }

    /**
     * 获取用户详情信息
     * @param
     * @return
     */
    @Login
    @GetMapping("/")
    @ApiOperation(value = "获取用户详情",notes = "获取用户详情",tags = "用户接口",httpMethod = "GET")
    public R getUserInfo(@RequestParam(name = "token") String token,@ApiIgnore @LoginUser User user){
        try {
            user.setPassword("");
        }catch (Exception e){
            return R.error(e.getMessage());
        }
        return R.ok().put("data",user);
    }

}
